﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace BulldogCMS.Data
{
    public static class Extensions
    {
        public static T GetAttributeValue<T>(this XElement el, XName name)
        {
            if (el.Attribute(name) != null)
            {
                object value = el.Attribute(name).Value;

                if (typeof(T).IsEnum)
                {
                    string enumName = value as string;
                    return (T)(object)Enum.Parse(typeof(T), enumName);
                }

                if (typeof(T) == typeof(string)) return (T)value;
                if (typeof(T) == typeof(DateTime))
                {
                    return (T)(object)DateTime.Parse((string)value);
                }
                if (typeof(T) == typeof(int))
                {
                    object number = int.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(ushort))
                {
                    object number = ushort.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(long))
                {
                    object number = long.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(uint))
                {
                    object number = uint.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(Guid))
                {
                    object guid = Guid.Parse((string)value);
                    return (T)guid;
                }
                if (typeof(T) == typeof(byte))
                {
                    object number = byte.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(bool))
                {
                    string sValue = (value as string).ToLower();
                    if (sValue == "true")
                    {
                        return (T)(object)true;
                    }
                    else
                    {
                        return (T)(object)false;
                    }
                }

                return default(T);
            }
            else
            {
                return default(T);
            }
        }

        public static T GetElementValue<T>(this XElement el, XName name)
        {
            var pokus = el.Element(name.LocalName);
            if (el.Element(name) != null)
            {
                object value = el.Element(name).Value;

                if (typeof(T) == typeof(string)) return (T)value;
                if (typeof(T) == typeof(byte))
                {
                    object number = byte.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(ushort))
                {
                    object number = ushort.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(uint))
                {
                    object number = uint.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(int))
                {
                    object number = int.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(long))
                {
                    object number = long.Parse((string)value);
                    return (T)number;
                }
                if (typeof(T) == typeof(bool))
                {
                    string sValue = (value as string).ToLower();
                    if (sValue == "true")
                    {
                        return (T)(object)true;
                    }
                    else
                    {
                        return (T)(object)false;
                    }
                }

                return default(T);
            }
            else
            {
                return default(T);
            }
        }

        public static T GetAttributeValueEnum<T>(this XElement el, string name, params string[] items)
        {
            int index = -1;
            string value = el.Attribute(name).Value;
            foreach (string item in items)
            {
                index++;
                if (item == value) break;
            }
            return (T)Enum.ToObject(typeof(T), index + 1);
        }

        public static void SetAttributeOrLeaveEmpty<T>(this XElement el, string name, T obj)
        {
            if (obj != null)
            {
                if (typeof(T) == typeof(bool))
                {
                    bool value = (bool)(object)obj;
                    if (value)
                    {
                        el.Add(new XAttribute(name, "true"));
                    }
                }
                else
                {
                    el.Add(new XAttribute(name, obj.ToString()));
                }
            }
        }

        public static string Fill(this string text, params object[] objs)
        {
            return string.Format(text, objs);
        }

    }
}
